'use strict';
// 申请开通视图模块
import {
    titileInputInit
} from '../components/titleInput.js';

var $verifyBtn,$phone,$verification_code,$newPhone;
var daojs = 60;
// 视图初始化
function viewInit() {
    $verifyBtn = $("#verifyBtn");
    $phone = $("#phone");
    $verification_code = $("#verification_code");
    $newPhone = $("#new-phone");
    titileInputInit();
    bindViewEvent();
    $("#fanhui").on("touchstart", function() {
        history.back();
    });
}


// 验证码倒计时
function djsfunc() {
    var handle = setInterval(function() {
        $verifyBtn.text("重新获取(" + daojs + "S)");
        daojs--;
        if (daojs == 0) {
            clearInterval(handle);
            $verifyBtn.attr("weui-btn weui-btn_plain-primary");
            $verifyBtn.text("重新获取验证码");
            daojs = 60;
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }
    }, 1000);
}

// 验证输入框 是否允许提交
function checkedInputValue() {
    if($phone.val().length == 11 && $verification_code.val().length > 3 && $newPhone.val().length == 11){
        $("#submit_btn").attr("class","weui-btn weui-btn_primary");
    }else{
        $("#submit_btn").attr("class","weui-btn weui-btn_disabled weui-btn_default ui-link");
    }
}


// 绑定事件
function bindViewEvent () {
    $("#phone").on("input",function  () {
        var oldPhone = $(this).val();
        if(oldPhone.length == 11){
            $verifyBtn.attr("class","weui-btn weui-btn_plain-primary");
        }else{
            $verifyBtn.attr("class","weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
    });

    $(".weui-cells_form").on("input",checkedInputValue);
}

export{
  viewInit,
  djsfunc 
}